/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.common;

import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import locusts.common.entities.Actor;
import locusts.common.entities.Entity;
import locusts.common.entities.EntityList;
import locusts.common.entities.EntityTypeList;

/**
 *
 * @author Hamish Morgan
 */
public class World implements Serializable, Cloneable {

    public static enum State {

        INITIALISING,
        RUNNING,
        GAME_OVER
    }
    private State state = State.INITIALISING;
    private EntityList entities;
    private EntityTypeList types;
    private int width;
    private int height;
    private transient boolean modified;

    protected World(int width, int height, EntityList entities,
            EntityTypeList types) {
        this.entities = entities;
        this.types = types;
        this.width = width;
        this.height = height;
        modified = true;
        state = State.RUNNING;
    }

    public World(int width, int height) {
        this(width, height, new EntityList(), new EntityTypeList());
    }

    private World(World other) {
        this.width = other.width;
        this.height = other.height;
        this.entities = other.entities.clone();
        this.types = other.types.clone();
        this.state = other.state;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeInt(state.ordinal());
        out.writeObject(entities);
        out.writeObject(types);
        out.writeInt(width);
        out.writeInt(height);

    }

    private void readObject(ObjectInputStream in) throws IOException,
            ClassNotFoundException {
        state = State.values()[in.readInt()];
        entities = (EntityList) in.readObject();
        types = (EntityTypeList) in.readObject();
        width = in.readInt();
        height = in.readInt();
    }

    public void update(double period) {

        final double momentum = 0.50f;
        final double tickMomentum = Math.pow(momentum, period);
//        final double energyDepletion = Math.pow(0.95, period);
//
        for (Entity e : entities) {

//            e.setEnergy(e.getEnergy() * energyDepletion);
//
            if (!(e instanceof Actor))
                continue;
            Actor a = (Actor) e;

            if (a.getVX() == 0 && a.getVY() == 0)
                continue;

            a.setX(a.getX() + a.getVX() * period);
            a.setY(a.getY() + a.getVY() * period);

            a.setVelocityX(a.getVX() * tickMomentum + a.getAX() * period);
            a.setVelocityY(a.getVY() * tickMomentum + a.getAY() * period);

            a.setOrientation(a.getHeading());
        }
//        }
    }

    public void setState(State state) {
        if (this.state != state) {
            System.out.println("State changed to: " + state);
            this.state = state;
            setModified();
        }
    }

    public State getState() {
        return state;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        if (width != this.width) {
            this.width = width;
            setModified();
        }
    }

    public final int getHeight() {
        return height;
    }

    public final void setHeight(final int height) {
        if (height != this.height) {
            this.height = height;
            setModified();
        }
    }

    public Rectangle2D getBounds() {
        return new Rectangle2D.Double(0, 0, width, height);
    }

    public final EntityList getEntities() {
        return entities;
    }

    public final void setEntities(EntityList entities) {
        this.entities = entities;
    }

    public EntityTypeList getTypes() {
        return types;
    }

    public void setTypes(EntityTypeList types) {
        this.types = types;
    }

    public final void setModified() {
        modified = true;
    }

    public final void clearModified() {
        modified = false;
    }

    public final boolean isModified() {
        return modified;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final World other = (World) obj;
        if (this.width != other.width) return false;
        if (this.height != other.height) return false;
        if (this.entities != other.entities &&
                (this.entities == null ||
                !this.entities.equals(other.entities))) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 29 * hash +
                (this.entities != null ? this.entities.hashCode() : 0);
        hash = 29 * hash + this.width;
        hash = 29 * hash + this.height;
        return hash;
    }

    @Override
    public World clone() {
        return new World(this);
    }
}
